//
//  PaperAssessmentCollectionView.m
//  智云题库
//
//  Created by 毕武侠 on 2017/4/5.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperAssessmentCollectionView.h"
#import "PaperAssessmentCollectionReusableView.h"
#import "PaperAssessmentCardModel.h"
#import "PaperAssessmentCollectionViewCell.h"

@interface PaperAssessmentCollectionView ()



@end

@implementation PaperAssessmentCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        [self registerCellClass:@"PaperAssessmentCollectionViewCell" cellIdentifier:@"PaperAssessmentCollectionViewCellID"];
        [self registerClass:[PaperAssessmentCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PaperAssessmentCollectionReusableViewID"];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self registerCellClass:@"PaperAssessmentCollectionViewCell" cellIdentifier:@"PaperAssessmentCollectionViewCellID"];
        [self registerClass:[PaperAssessmentCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PaperAssessmentCollectionReusableViewID"];
    }
    return self;
}

#pragma mark  collectionView delegate dataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.sourceData.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    PaperAssessmentCardModel *model = self.sourceData[section];
    return model.ques_data.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return  CGSizeMake(40.0f, 40.0f);
}

//这个方法是返回 Header的大小 size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(SCREEN_WIDTH, 32.0f);
}

//这个也是最重要的方法 获取Header的 方法。
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = @"PaperAssessmentCollectionReusableViewID";
    PaperAssessmentCollectionReusableView *head = (PaperAssessmentCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    PaperAssessmentCardModel *model = self.sourceData[indexPath.section];
    head.title = model.module_name;
    return head;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PaperAssessmentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:
                                    @"PaperAssessmentCollectionViewCellID" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[PaperAssessmentCollectionViewCell alloc] init];
    }
    
    PaperAssessmentCardModel *model = self.sourceData[indexPath.section];
    PaperAssessmentCardAnswerModel *cardModel = model.ques_data[indexPath.row];
    
    cell.indexPath      = indexPath;
    [cell bindModel:cardModel];
    return cell;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
